﻿Imports System.Windows.Controls.Primitives

Public Class PianoKeyControl
    Inherits System.Windows.Controls.Control

    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(PianoKeyControl), New FrameworkPropertyMetadata(GetType(PianoKeyControl)))
    End Sub



    Public Property IsBlackKey As Boolean
        Get
            Return DirectCast(GetValue(IsBlackKeyProperty), Boolean)
        End Get
        Set(ByVal value As Boolean)
            SetValue(IsBlackKeyProperty, value)
        End Set
    End Property

    Public Shared ReadOnly IsBlackKeyProperty As DependencyProperty = DependencyProperty.Register( _
        "IsBlackKey", GetType(Boolean), GetType(PianoKeyControl), _
        New FrameworkPropertyMetadata(False, FrameworkPropertyMetadataOptions.AffectsMeasure, _
            Sub(d, e) DirectCast(d, PianoKeyControl).OnIsBlackKeyChanged( _
                DirectCast(e.OldValue, Boolean), DirectCast(e.NewValue, Boolean))), _
        Function(o) True)

    Protected Overridable Sub OnIsBlackKeyChanged(oldValue As Boolean, newValue As Boolean)

    End Sub



    Public Property XShift As Double
        Get
            Return DirectCast(GetValue(XShiftProperty), Double)
        End Get
        Set(ByVal value As Double)
            SetValue(XShiftProperty, value)
        End Set
    End Property

    Public Shared ReadOnly XShiftProperty As DependencyProperty = DependencyProperty.Register( _
        "XShift", GetType(Double), GetType(PianoKeyControl), _
        New FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsArrange))



    Public Property IsPressed As Boolean
        Get
            Return DirectCast(GetValue(IsPressedProperty), Boolean)
        End Get
        Set(ByVal value As Boolean)
            SetValue(IsPressedProperty, value)
        End Set
    End Property

    Public Shared ReadOnly IsPressedProperty As DependencyProperty = DependencyProperty.Register( _
        "IsPressed", GetType(Boolean), GetType(PianoKeyControl),
        New FrameworkPropertyMetadata(False))



    Protected Overrides Function MeasureOverride(constraint As Size) As Size
        Dim measuresize = If(IsBlackKey, New Size(11, 36), New Size(17, 60))
        Dim returnsize = If(IsBlackKey, New Size(0, 60), New Size(17, 60))
        If VisualChildrenCount > 0 Then
            Dim child = TryCast(GetVisualChild(0), UIElement)
            If child IsNot Nothing Then
                child.Measure(measuresize)
            End If
        End If
        Return returnsize
    End Function

    Protected Overrides Function ArrangeOverride(arrangeBounds As Size) As Size
        Dim arrangerect = If(IsBlackKey, New Rect(XShift - 11 / 2, 0, 11, 36), New Rect(0, 0, 17, 60))
        Dim returnsize = If(IsBlackKey, New Size(0, 60), New Size(17, 60))
        If VisualChildrenCount > 0 Then
            Dim child = TryCast(GetVisualChild(0), UIElement)
            If child IsNot Nothing Then
                child.Arrange(arrangerect)
            End If
        End If
        Return returnsize
    End Function

    Protected Overrides Sub OnMouseLeftButtonDown(e As MouseButtonEventArgs)
        MyBase.OnMouseLeftButtonDown(e)
        CaptureMouse()
        IsPressed = True
    End Sub

    Protected Overrides Sub OnMouseLeftButtonUp(e As MouseButtonEventArgs)
        MyBase.OnMouseLeftButtonUp(e)
        ReleaseMouseCapture()
        IsPressed = False
    End Sub

End Class
